import {createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { defineAsyncComponent } from 'vue'
import utils from "../router/util";
// const path = require('path')

const luckyTiger = () => import('../views/luckyTiger.vue')
const vertyCust = () => import('../views/vertyCust.vue')
const saleRecord = () => import('../views/saleRecord.vue')
const loginSale = () => import('../views/loginSale.vue')
const indexSurvey = () => import('../views/indexSurvey.vue')
const surveyRender = () => import('../views/surveyRender.vue')
const randomImg = () => import('../views/randomImg.vue')
const index = () => import('../views/index.vue')
const booking = () => import('../views/adBooking.vue')
const luckyHouse = ()=> import('../views/luckyHouse.vue')
const apartmentPic = ()=>import('../views/apartmentPic.vue')  // 户型图一览主页
const complexDetail = ()=>import('../views/complexDetail.vue') // 户型图详情页
const peopleSing = ()=>import('../views/peopleSing.vue') // K歌录入
const singRank = ()=>import('../views/singRank.vue')  // K歌排行
const loginMail = ()=>import('../views/loginMail.vue') // 登录鉴权
const makeComplex = ()=>import('../views/makeComplex.vue') // 小区录入
const installPage = ()=>import('../views/installPage.vue')  // 装维录入按ID
const behaveRecord = ()=>import('../views/behaveRecord.vue') // 新的行为录入页面（待定）
const loginAction = ()=>import('../views/shadow/loginAction.vue') // 匹配行为录入的登录页
const actionRank = ()=>import('../views/shadow/actionRank.vue') // 匹配行为录入的排行页

const router = createRouter({
    history: createWebHashHistory(), // 如果是根目录部署请配置为 /
    routes: [
        // route -> routes
        {
            path: '/',
            name: 'index',
            component: index,
        },
        {
            path: '/random-img',
            name: 'random-img',
            component: randomImg,
        },
        {
            path: '/booking',
            name:'booking',
            component: booking
        },
        {
            path: '/ren_survey',
            name: 'surveyRender',
            component: surveyRender
        },
        {
            path: '/survey',
            name: 'indexSurvey',
            component: indexSurvey
        },
        {
            path: '/loginSale',
            name: 'loginSale',
            component: loginSale
        },
        {
            path: '/saleRecord',
            name: 'saleRecord',
            props({query:{name, road}}){
                console.log('路由参数', name, road);
                   return {name, road}
            },
            component: saleRecord,
            meta: {needLogin: true}
        },
        {
            path: '/vertyCust',
            name: 'vertyCust',
            props({query:{name, road}}){
                console.log('路由参数', name, road);
                   return {name, road}
            },
            component: vertyCust,
            meta: {needLogin: true}
        },
        {
            path: '/luckyHouse',
            name: 'luckyHouse',        
            component: luckyHouse,
        },
        {
            path: '/luckyTiger',
            name: 'luckyTiger',        
            component: luckyTiger
        },
        {
            path: '/apartmentPic',
            name: 'apartmentPic',
            component: apartmentPic
        },
        {
            path: '/complexDetail',
            name: 'complexDetail',
            component: complexDetail
        },
        {
            path: '/peopleSing',
            name: 'peopleSing',
            component: peopleSing
        },
        {
            path: '/singRank',
            name: 'singRank',
            component: singRank
        },
        {
            path: '/loginMail',
            name: 'loginMail',
            component: loginMail
        },
        {
            path: '/makeComplex',
            name: 'makeComplex',
            component: makeComplex
        },
        {
            path: '/installPage',
            name: 'installPage',
            component: installPage,
            meta:{title:'江门广电网络'}
        },
        {
            path: '/behaveRecord',
            name: 'behaveRecord',
            component: behaveRecord,
            meta: {title: '争做文明“观察员”随手拍云平台'}
        },
        {
            path: '/loginAction',
            name: 'loginAction',
            component: loginAction,
            meta: {title: '争做文明“观察员”随手拍云平台'}
        },
        {
            path: '/actionRank',
            name: 'actionRank',
            component: actionRank
        },
        {
            path: '/*',
            redirect: '/index',
        },
    ],
})
router.beforeEach((to, from, next)=>{
    document.title = to.meta.title ? to.meta.title : '江门广电网络';
    // console.log(to, from)
    next()
})
router.afterEach((to, from)=>{
    // console.log('afterEach')
})


// 全局守卫
router.beforeEach((to, from) => {
    let userLogin = utils.getCookie('mytoken').length>10
    console.log('是否登录', userLogin)
    if (to.meta.needLogin && !userLogin) {
      // need to login
      return { name: "loginSale" };
    }
  });

export default router


// Component "default" in record with path "/luckyTiger" is defined using 
// "defineAsyncComponent()". Write "() => import('./MyPage.vue')" instead of 
// "defineAsyncComponent(() => import('./MyPage.vue'))".